#! /usr/bin/perl

use POSIX;
use Math::BigFloat;



sub promptUser {

   #-------------------------------------------------------------------#
   #  two possible input arguments - $promptString, and $defaultValue  #
   #  make the input arguments local variables.                        #
   #-------------------------------------------------------------------#

   local($promptString,$defaultValue) = @_;

   #-------------------------------------------------------------------#
   #  if there is a default value, use the first print statement; if   #
   #  no default is provided, print the second string.                 #
   #-------------------------------------------------------------------#

   if ($defaultValue) {
      print $promptString, "[", $defaultValue, "]: ";
   } else {
      print $promptString, ": ";
   }

   $| = 1;               # force a flush after our print
   $_ = <STDIN>;         # get the input from STDIN (presumably the keyboard)


   #------------------------------------------------------------------#
   # remove the newline character from the end of the input the user  #
   # gave us.                                                         #
   #------------------------------------------------------------------#

   chomp;

   #-----------------------------------------------------------------#
   #  if we had a $default value, and the user gave us input, then   #
   #  return the input; if we had a default, and they gave us no     #
   #  no input, return the $defaultValue.                            #
   #                                                                 # 
   #  if we did not have a default value, then just return whatever  #
   #  the user gave us.  if they just hit the <enter> key,           #
   #  the calling routine will have to deal with that.               #
   #-----------------------------------------------------------------#

   if ("$defaultValue") {
      return $_ ? $_ : $defaultValue;    # return $_ if it has a value
   } else {
      return $_;
   }
}



if ($#ARGV+1 != 6) {
    print "Error: incorrect number of command line args\n";
    print "args: (data file) (root name) (n bins) (start) (end) (step)\n";
    exit(1);
}

#$data="speeds2004.dat";
#$name="s2004C";
#$bins=5;
#$start=Math::BigFloat->new(13.00);
#$end=Math::BigFloat->new(13.10);
#$step=Math::BigFloat->new(0.001);
$data=$ARGV[0];
$name=$ARGV[1];
$bins=$ARGV[2];
$start=Math::BigFloat->new($ARGV[3]);
$end=Math::BigFloat->new($ARGV[4]);
$step=Math::BigFloat->new($ARGV[5]);
$nFolds = ($end-$start)/$step;
if ($nFolds > 20) {
    print "More than 20 folds requested ($nFolds)!\n";
    $ans = &promptUser("- continue? (y/n)  ","y");
    if ($ans ne "y") {
        print "exiting :(\n";
        exit(1);
    }
}

print "ARGS: $data $name $bins $start $end $step (nfolds = $nFolds)\n";


printf("- Data = %s, nbins = %d\n", $data, $bins);
printf("- Folding from %.3f to %.3f with step %.3f\n", $start, $end, $step);

$i = 0;
for ($fold = $start; $fold <= $end; $fold += $step) {
    $command = "perl foldTimeSeries.pl $data $fold $bins $name";
    print "[$i] $command\n";
    system($command) == 0
        or die "folding failed\n";
    $i++;
}

